package com.jerry.Advice;

import com.jerry.errors.BusinessException;
import com.jerry.response.CommonResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.text.MessageFormat;
import java.util.Arrays;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    /*@ExceptionHandler(BusinessException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public Object handleException(BusinessException exception) {
        exception.printStackTrace();
        log.info(exception.getMessage());
        log.error(formatExceptionMsg(exception.getMessage(), exception));
        return new CommonResponse(500, exception.getMessage(), null);
    }*/


    public static String formatExceptionMsg(String errMsg, Throwable e) {
        StackTraceElement[] stackTrace = e.getStackTrace();
        String stackTraceString = Arrays.toString(stackTrace).replace("[",
                "").replace("]", "").replace(",", "\n\t");
        return MessageFormat.format(
                "异常标题: {0} \n" +
                        "文件名: {1} \n" +
                        "类名: {2}\n" +
                        "方法名: {3}\n " +
                        "抛出异常行号: {4}\n " +
                        "异常栈信息: {5}\n ",
                errMsg,
                e.getStackTrace()[0].getFileName(),
                e.getStackTrace()[0].getClassName(),
                e.getStackTrace()[0].getMethodName(),
                e.getStackTrace()[0].getLineNumber(),
                stackTraceString
        );
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public Object handleException(MethodArgumentNotValidException exception) {
        FieldError fieldError = exception.getBindingResult().getFieldError();
        assert fieldError != null;
        log.error(formatExceptionMsg(fieldError.getField() + " : " +
                fieldError.getDefaultMessage(), exception));
        return new CommonResponse(500, fieldError.getDefaultMessage(), null);
    }

    /*@ExceptionHandler(RuntimeException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public Object handlerRuntimeException(RuntimeException ex) {
        ex.printStackTrace();
        return new CommonResponse(500, "请求出现异常", null);
    }*/

    /*@ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public Object handlerException(Exception ex) {
        ex.printStackTrace();
        return new CommonResponse(500, "请求出现异常", null);
    }*/

}
